//
//  WFDatePickerViewController.m
//  meizhuang
//
//  Created by hzduanjiashun on 16/7/11.
//  Copyright © 2016年 netease. All rights reserved.
//

#import "WFDatePickerViewController.h"

@interface WFDatePickerViewController ()

@end

@implementation WFDatePickerViewController
@synthesize date = _date;

- (void)loadView {
    [super loadView];
    
    self.contentHeight = 240;
    
    _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    _datePicker.datePickerMode = UIDatePickerModeDate;
    
    if (_maxDate)
        _datePicker.maximumDate = _maxDate;
    if (_minDate)
        _datePicker.minimumDate = _minDate;
    if (_date)
        [_datePicker setDate:_date animated:NO];
    
    [self.contentView addSubview:_datePicker];
    
    _cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton setTitleColor:[UIColor themeColor]
                        forState:UIControlStateNormal];
    [_cancelButton addTarget:self
                      action:@selector(cancelButtonClicked)
            forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_cancelButton];
    
    _okButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _okButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [_okButton setTitle:@"确定"
               forState:UIControlStateNormal];
    [_okButton setTitleColor:[UIColor themeColor]
                    forState:UIControlStateNormal];
    [_okButton addTarget:self
                  action:@selector(okButtonClicked)
        forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_okButton];
    
    [_datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 44, 0));
    }];
    [_cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.equalTo(self.cancelButton.superview);
        make.top.equalTo(self.datePicker.mas_bottom);
        make.width.equalTo(self.okButton);
        make.right.equalTo(self.okButton.mas_left);
    }];
    [_okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.okButton.superview);
        make.top.equalTo(self.cancelButton);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)setDate:(NSDate *)date {
    if (_datePicker)
        [_datePicker setDate:date animated:YES];
    _date = date;
}

- (NSDate *)date {
    return self.datePicker.date;
}

- (void)setMaxDate:(NSDate *)maxDate {
    _maxDate = maxDate;
    if (_datePicker)
        _datePicker.maximumDate = maxDate;
}

- (void)setMinDate:(NSDate *)minDate {
    _minDate = minDate;
    if (_datePicker)
        _datePicker.minimumDate = minDate;
}

- (void)cancelButtonClicked {
    [self dismissViewControllerAnimated:YES completion:NULL];
    if ([_delegate respondsToSelector:@selector(datePickerViewControllerDidCancel:)]) {
        [_delegate datePickerViewControllerDidCancel:self];
    }
}

- (void)okButtonClicked {
    [self dismissViewControllerAnimated:YES completion:NULL];
    if ([_delegate respondsToSelector:@selector(datePickerViewController:didSelectDate:)]) {
        [_delegate datePickerViewController:self didSelectDate:self.datePicker.date];
    }
}

@end
